﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Common.Helper
{
    public class FileHelper
    {
        public FileHelper()
        {
            StrFile = "";
            StrFoder = "";
            ExtensionFile = "";
        }

        /// <summary>
        /// get or set Extension
        /// </summary>
        public static string ExtensionFile { get; set; }

        /// <summary>
        /// get or set Forder
        /// </summary>
        public static string StrFoder { get; set; }

        /// <summary>
        /// get or set File
        /// </summary>
        public static string StrFile { get; set; }

        /// <summary>
        /// style time
        /// </summary>
        public enum StyleFile
        {
            HourMinuteSecond,
            YearMonthDaySecond
        }
        /// <summary>
        /// style forder
        /// </summary>
        public enum StyleFoder
        {
            YearMonthDay, YearMonth
        }


        public static bool FileImage(string fileName)
        {

            try
            {
                var strEx = new List<string> { ".bmp", ".gif", ".jpeg", ".jpg", ".png" };
                var extension = Path.GetExtension(fileName);
                if (extension != null && strEx.Contains(extension.ToLower()))
                {
                    var s = Path.GetExtension(fileName);
                    if (s != null) ExtensionFile = s.ToLower();
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public static void CreateFoder(string paht)
        {
            try
            {
                var dtinfo = new DirectoryInfo(paht);
                if (!dtinfo.Exists)
                {
                    dtinfo.Create();
                }
                StrFoder = paht;

            }
            catch
            {
                StrFoder = "";
            }

        }

        public static void CreateFoder(string paht, string styleFoder)
        {
            try
            {
                if (StyleFoder.YearMonthDay.ToString() == styleFoder)
                {
                    var strFoder1 = paht + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    var dtinfo = new DirectoryInfo(strFoder1);
                    if (!dtinfo.Exists)
                    {
                        dtinfo.Create();

                    }
                    StrFoder = strFoder1;
                }
                if (StyleFoder.YearMonth.ToString() == styleFoder)
                {
                    var strFoder1 = paht + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month;
                    var dtinfo = new DirectoryInfo(strFoder1);
                    if (!dtinfo.Exists)
                    {
                        dtinfo.Create();

                    }
                    StrFoder = strFoder1;
                }

            }
            catch
            {

                StrFoder = "";
            }


        }
        public static void DeleteFile(string path)
        {
            try
            {
                var file = new FileInfo(path);
                if (file.Exists)
                {
                    file.IsReadOnly = false;
                    file.Delete();
                }
            }
            catch (Exception exception)
            {
                //write log
                Console.Write(exception.Message);

            }
        }
        public static void CreateFile(string strpath, string styleFile, string strWordEnd)
        {
            try
            {
                var fi = "";
                if (StyleFile.HourMinuteSecond.ToString() == styleFile)
                {
                    while (true)
                    {
                        fi = strpath + "/" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second + "_" + strWordEnd;
                        var file = new FileInfo(fi);
                        if (!file.Exists)
                        {
                            break;

                        }

                    }
                }
                else
                {
                    if (StyleFile.YearMonthDaySecond.ToString() == styleFile)
                    {
                        while (true)
                        {
                            fi = strpath + "/" + DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Second + "_" + strWordEnd;
                            var file = new FileInfo(fi);
                            if (!file.Exists)
                            {
                                break;

                            }

                        }
                    }

                }
                StrFile = fi;
            }
            catch
            {
                StrFile = "";
            }

        }
    }

}
